2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_EditingPanelBase.h"
28 #include "jucer_JucerDocumentHolder.h"
30 //==============================================================================
31 class EditingPanelBase::MagnifierComponent
: public Component
34 MagnifierComponent (Component
* content_
)
35 : scaleFactor (1.0), content (content_
)
37 addAndMakeVisible (content
);
38 childBoundsChanged (content
);
41 void childBoundsChanged (Component
* child
)
43 const Rectangle
<int> childArea (getLocalArea (child
, child
->getLocalBounds()));
44 setSize (childArea
.getWidth(), childArea
.getHeight());
47 double getScaleFactor() const { return scaleFactor
; }
49 void setScaleFactor (double newScale
)
51 scaleFactor
= newScale
;
52 content
->setTransform (AffineTransform::scale ((float) scaleFactor
,
53 (float) scaleFactor
));
58 ScopedPointer
<Component
> content
;
61 //==============================================================================
62 class ZoomingViewport
: public Viewport
65 ZoomingViewport (EditingPanelBase
* const panel_
)
73 void mouseWheelMove (const MouseEvent
& e
, float ix
, float iy
)
75 if (e
.mods
.isCtrlDown() || e
.mods
.isAltDown())
77 const double factor
= (iy
> 0) ? 2.0 : 0.5;
79 panel
->setZoom (panel
->getZoom() * factor
, e
.x
, e
.y
);
83 Viewport::mouseWheelMove (e
, ix
, iy
);
87 void dragKeyHeldDown (const bool isKeyDown
)
89 if (isSpaceDown
!= isKeyDown
)
91 isSpaceDown
= isKeyDown
;
95 DraggerOverlayComp
* const dc
= new DraggerOverlayComp();
96 addAndMakeVisible (dc
);
97 dc
->setBounds (0, 0, getWidth(), getHeight());
101 for (int i
= getNumChildComponents(); --i
>= 0;)
103 if (dynamic_cast <DraggerOverlayComp
*> (getChildComponent (i
)) != 0)
105 delete getChildComponent (i
);
113 EditingPanelBase
* const panel
;
116 //==============================================================================
117 class DraggerOverlayComp
: public Component
122 setMouseCursor (MouseCursor::DraggingHandCursor
);
123 setAlwaysOnTop (true);
126 ~DraggerOverlayComp()
130 void mouseDown (const MouseEvent
& e
)
132 Viewport
* viewport
= findParentComponentOfClass ((Viewport
*) 0);
136 startX
= viewport
->getViewPositionX();
137 startY
= viewport
->getViewPositionY();
141 void mouseDrag (const MouseEvent
& e
)
143 Viewport
* viewport
= findParentComponentOfClass ((Viewport
*) 0);
147 viewport
->setViewPosition (jlimit (0, jmax (0, viewport
->getViewedComponent()->getWidth() - viewport
->getViewWidth()),
148 startX
- e
.getDistanceFromDragStartX()),
149 jlimit (0, jmax (0, viewport
->getViewedComponent()->getHeight() - viewport
->getViewHeight()),
150 startY
- e
.getDistanceFromDragStartY()));
160 //==============================================================================
161 EditingPanelBase::EditingPanelBase (JucerDocument
& document_
,
162 Component
* propsPanel_
,
163 Component
* editorComp
)
164 : document (document_
),
166 propsPanel (propsPanel_
)
168 addAndMakeVisible (viewport
= new ZoomingViewport (this));
169 addAndMakeVisible (propsPanel
);
171 viewport
->setViewedComponent (magnifier
= new MagnifierComponent (editor
));
174 EditingPanelBase::~EditingPanelBase()
179 void EditingPanelBase::resized()
181 const int contentW
= jmax (1, getWidth() - 260);
183 propsPanel
->setBounds (contentW
+ 4, 4, jmax (100, getWidth() - contentW
- 8), getHeight() - 8);
185 viewport
->setBounds (4, 4, contentW
- 8, getHeight() - 8);
187 if (document
.isFixedSize())
188 editor
->setSize (jmax (document
.getInitialWidth(),
189 roundToInt ((viewport
->getWidth() - viewport
->getScrollBarThickness()) / getZoom())),
190 jmax (document
.getInitialHeight(),
191 roundToInt ((viewport
->getHeight() - viewport
->getScrollBarThickness()) / getZoom())));
193 editor
->setSize (viewport
->getWidth(),
194 viewport
->getHeight());
197 void EditingPanelBase::visibilityChanged()
201 updatePropertiesList();
203 if (getParentComponent() != 0)
206 JucerDocumentHolder
* const cdh
= dynamic_cast <JucerDocumentHolder
*> (getParentComponent()->getParentComponent());
209 cdh
->setViewportToLastPos (viewport
, *this);
216 if (getParentComponent() != 0)
218 JucerDocumentHolder
* const cdh
= dynamic_cast <JucerDocumentHolder
*> (getParentComponent()->getParentComponent());
221 cdh
->storeLastViewportPos (viewport
, *this);
225 editor
->setVisible (isVisible());
228 double EditingPanelBase::getZoom() const
230 return magnifier
->getScaleFactor();
233 void EditingPanelBase::setZoom (double newScale
)
235 setZoom (jlimit (1.0 / 8.0, 16.0, newScale
),
236 viewport
->getWidth() / 2,
237 viewport
->getHeight() / 2);
240 void EditingPanelBase::setZoom (double newScale
, int anchorX
, int anchorY
)
242 Point
<int> anchor (editor
->getLocalPoint (viewport
, Point
<int> (anchorX
, anchorY
)));
244 magnifier
->setScaleFactor (newScale
);
247 anchor
= viewport
->getLocalPoint (editor
, anchor
);
249 viewport
->setViewPosition (jlimit (0, jmax (0, viewport
->getViewedComponent()->getWidth() - viewport
->getViewWidth()),
250 viewport
->getViewPositionX() + anchor
.getX() - anchorX
),
251 jlimit (0, jmax (0, viewport
->getViewedComponent()->getHeight() - viewport
->getViewHeight()),
252 viewport
->getViewPositionY() + anchor
.getY() - anchorY
));
255 void EditingPanelBase::xyToTargetXY (int& x
, int& y
) const
257 Point
<int> pos (editor
->getLocalPoint (this, Point
<int> (x
, y
)));
262 void EditingPanelBase::dragKeyHeldDown (bool isKeyDown
)
264 ((ZoomingViewport
*) viewport
)->dragKeyHeldDown (isKeyDown
);